/**
 * 
 */
package npap.cater.unr.edu.client.scaffold.place.customized;

import com.google.gwt.place.shared.PlaceTokenizer;
import com.google.gwt.place.shared.Prefix;
import com.google.web.bindery.requestfactory.shared.EntityProxy;
import com.google.web.bindery.requestfactory.shared.EntityProxyId;
import com.google.web.bindery.requestfactory.shared.RequestFactory;

import npap.cater.unr.edu.client.proxy.ProjectProxy;
import npap.cater.unr.edu.client.scaffold.place.ProxyPlace;
import npap.cater.unr.edu.client.scaffold.place.ProxyPlace.Operation;

/**
 * @author Hao
 *
 */
public class AppPlace extends ProxyPlace {
	/**
	 * @uml.property  name="projectID"
	 */
	private Integer projectID;
	/**
	 * @uml.property  name="projectName"
	 */
	private String projectName;
	
	/**
	 * @return
	 * @uml.property  name="projectID"
	 */
	public Integer getProjectID() {
		return projectID;
	}

	/**
	 * @param projectID
	 * @uml.property  name="projectID"
	 */
	public void setProjectID(Integer projectID) {
		this.projectID = projectID;
	}

	/**
	 * @return
	 * @uml.property  name="projectName"
	 */
	public String getProjectName() {
		return projectName;
	}

	/**
	 * @param projectName
	 * @uml.property  name="projectName"
	 */
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	/**
	 * Tokenizer.
	 */
	@Prefix("p")
	public static class Tokenizer implements PlaceTokenizer<AppPlace> {
		private static final String SEPARATOR = "!";
		private final RequestFactory requests;

		public Tokenizer(RequestFactory requests) {
			this.requests = requests;
		}

		public AppPlace getPlace(String token) {
			String bits[] = token.split(SEPARATOR);
			Operation operation = Operation.valueOf(bits[3]);
			if (Operation.CREATE == operation) {
				return new AppPlace(requests.getProxyClass(bits[0]),operation, Integer.valueOf(bits[1]),bits[2]);
			}
			if (Operation.LIST == operation)
			{
				return new AppPlace(requests.getProxyClass(bits[0]), operation, Integer.valueOf(bits[1]),bits[2]);
			}
			return new AppPlace(requests.getProxyId(bits[0]), operation, Integer.valueOf(bits[1]),bits[2]);
		}

		public String getToken(AppPlace place) {
			if (Operation.CREATE == place.getOperation()) {
				return requests.getHistoryToken(place.getProxyId())+SEPARATOR + place.getProjectID()+SEPARATOR+place.projectName+ SEPARATOR + place.getOperation();
			}
			if (Operation.LIST == place.getOperation())
			{
				return requests.getHistoryToken(place.getProxyClass())+SEPARATOR + place.projectID+SEPARATOR+place.projectName+ SEPARATOR + place.getOperation();
			}
			return requests.getHistoryToken(place.getProxyId())+SEPARATOR + place.projectID+SEPARATOR+place.projectName + SEPARATOR + place.getOperation();
		}
	}



	public AppPlace(EntityProxyId<?> proxyId, Operation operation, Integer projectID, String projectName) {
		super(proxyId, operation);
		// TODO Auto-generated constructor stub
		this.projectID=projectID;
		this.projectName=projectName;
	}

	public AppPlace(EntityProxyId<?> record, Integer projectID, String projectName) {
		super(record);
		// TODO Auto-generated constructor stub
		this.projectID=projectID;
		this.projectName=projectName;
	}

	public AppPlace(Class<? extends EntityProxy> proxyClass, Integer projectID, String projectName) {
		super(proxyClass);
//		 TODO Auto-generated constructor stub
		this.projectID=projectID;
		this.projectName=projectName;
	}

	public AppPlace(Class<? extends EntityProxy> proxyClass, Operation list,
			Integer projectID2, String projectName2) {
		super(proxyClass);
//		 TODO Auto-generated constructor stub
		this.operation=list;
		this.projectID=projectID2;
		this.projectName=projectName2;
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (!super.equals(obj))
			return false;
		if (!(obj instanceof AppPlace)) {
			return false;
		}
		if (this == obj) {
			return true;
		}

		AppPlace other = (AppPlace) obj;
		
		if (projectID == null) {
			if (other.projectID != null) {
				return false;
			}
		} else if (!projectID.equals(other.projectID)) {
			return false;
		}
		if (projectName == null) {
			if (other.projectName != null) {
				return false;
			}
		} else if (!projectName.equals(other.projectName)) {
			return false;
		}
		return true;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		final int prime = 31;
		int superhash=super.hashCode();
		superhash=prime * superhash + ((projectID == null) ? 0 : projectID.hashCode());
		superhash=prime * superhash + ((projectName == null) ? 0 : projectName.hashCode());
		return superhash;
		
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString()+"projectID="+projectID+"projectName="+projectName;
	}
	
	
	

}
